import * as fs from 'fs'
import mock from 'mock-fs'
import {assert, expect} from 'chai'
import * as util from '../src/util'

describe('emptyDir', () => {
    beforeEach(() => {
        mock({
            'emptyDir': {},
            'dirWithFiles': {
                'file1.txt': 'content1',
                'file2.txt': 'content2'
            },
            'dirWithSubdirs': {
                'subdir1': {},
                'subdir2': {}
            },
            'mixedDir': {
                'file1.txt': 'content1',
                'subdir1': {}
            },
            'nonExistentDir': {},
            'permissionDeniedDir': {
                'file1.txt': mock.file({ content: 'content1', mode: 0o000 })
            }
        });
    });

    afterEach(() => {
        mock.restore();
    });

    it('emptyDir should handle an empty directory without errors', () => {
        util.emptyDir('emptyDir');
        assert(fs.readdirSync('emptyDir').length==0);
    });

    it('emptyDir should delete all files in a directory', () => {
        util.emptyDir('dirWithFiles');
        assert(fs.readdirSync('dirWithFiles').length==0);
    });

    it('emptyDir should recursively empty all subdirectories', () => {
        util.emptyDir('dirWithSubdirs');
        assert(fs.readdirSync('dirWithSubdirs').length==2);
    });

    it('emptyDir should handle a directory with mixed content', () => {
        util.emptyDir('mixedDir');
        let strings = fs.readdirSync('mixedDir')
        assert(strings.length==1);
    });

    it('emptyDir should handle a non-existent directory without errors', () => {
        expect(() => util.emptyDir('nonExistentDir')).not.throw;
    });


});

describe('rmEmptyDir', () => {
    beforeEach(() => {
        mock({
            '/tmp1': {
                'emptyDir': {},
                'nonEmptyDir': {
                    'file.txt': 'content'
                },
                'nestedEmptyDir': {
                    'subDir': {}
                },
                'nestedNonEmptyDir': {
                    'subDir': {
                        'file.txt': 'content'
                    }
                },
                'topLevelEmptyDir': {}
            }
        });
    });

    afterEach(() => {
        mock.restore();
    });




    it('should not remove the top-level directory if it is empty', () => {
        util.rmEmptyDir('/tmp1/topLevelEmptyDir', 0);
        expect(fs.existsSync('/tmp1/topLevelEmptyDir')).equal(true);
    });
});


